<template>
  <div>
    <el-dialog
      :title="ruleForm.id ? '编辑部门' : '添加部门'"
      :visible="visible"
      @close="resetForm('ruleForm')"
    >
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="ruleForm.name" placeholder="2-10个字符" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model.trim="ruleForm.code" placeholder="2-10个字符" />
        </el-form-item>
        <el-form-item label="活动区域" prop="managerId">
          <el-select v-model="ruleForm.managerId" placeholder="请选择负责人">
            <el-option
              v-for="item in managerList"
              :key="item.id"
              :label="item.username"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="活动形式" prop="introduce">
          <el-input
            v-model="ruleForm.introduce"
            type="textarea"
            placeholder="1-100个字符"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
          >{{ ruleForm.id ? '更新' : '添加' }}</el-button>
          <el-button @click="resetForm('ruleForm')">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  addDepartmentRequest,
  getManagerRequest,
  getDepartmentByIdRequest,
  updateDepartmentByIdRequest
} from '@/api/department'
export default {
  name: '',
  props: {
    visible: {
      type: Boolean
    },
    list: {
      type: Array,
      default: () => []
    },
    currentId: {
      type: Number,
      default: 0
    }
  },
  data() {
    const validateName = (_, value, callback) => {
      const arr = this.ruleForm.id ? this.list.filter(item => item.id !== this.currentId) : this.list
      if (arr.some((item) => item.name === value)) {
        return callback(new Error('部门名称已存在'))
      }
      callback()
    }
    const validateCode = (_, value, callback) => {
      const arr = this.ruleForm.id ? this.list.filter(item => item.id !== this.currentId) : this.list
      if (arr.some((item) => item.code === value)) {
        return callback(new Error('部门编码已存在'))
      }
      callback()
    }
    return {
      // dialogVisible: false
      ruleForm: {
        name: '',
        code: '',
        managerId: '',
        introduce: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '部门名称不能为空',
            trigger: 'blur'
          },
          {
            min: 2,
            max: 10,
            message: '部门名称的长度为2-10个字符',
            trigger: 'blur'
          },
          {
            validator: validateName,
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '部门编码的长度为2-10个字符',
            trigger: 'blur'
          },
          {
            validator: validateCode,
            trigger: 'blur'
          }
        ],
        managerId: [
          {
            required: true,
            message: '部门负责人不能为空',
            trigger: ['blur', 'change']
          }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 100,
            message: '部门介绍的长度为1-100个字符',
            trigger: 'blur'
          }
        ]
      },
      managerList: []
    }
  },
  async created() {
    const res = await getManagerRequest()
    this.managerList = res.data
  },
  methods: {
    /*  openDialog() {
      this.dialogVisible = true
    } */
    submitForm(formName) {
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          if (!this.ruleForm.id) {
            const res = await addDepartmentRequest({
              ...this.ruleForm,
              pid: this.currentId
            })
            this.$message.success(res.message)
            this.resetForm(formName)
            this.$emit('okk')
          } else {
            const res = await updateDepartmentByIdRequest(this.ruleForm, this.currentId)
            this.$message.success(res.message)
            this.resetForm(formName)
            this.$emit('okk')
          }
        } else {
          console.log('error submit!!')
        }
      })
    },
    resetForm(formName) {
      this.$emit('update:visible', false)
      // 清除校验规则
      this.$refs[formName].resetFields()
      this.ruleForm = this.$options.data().ruleForm
    },
    async getDepartmentById() {
      const res = await getDepartmentByIdRequest(this.currentId)
      this.ruleForm = res.data
    }
  }
}
</script>
<style lang='scss'  scoped>
::v-deep.el-select {
  width: 100%;
}
</style>
